﻿
//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////

#include "ModelLineSnapStrategy.h"
#include "IElement.h"
#include "ICurve3d.h"
#include "IModelView.h"
#include "IModelLine.h"
#include "IDirectShape.h"
#include "Vector3dUtils.h"
#include "GraphicsNodeId.h"
#include "IGraphicsPoint.h"
#include "IGenericElement.h"
#include "IGenericElement.h"
#include "IGraphicsCurve3d.h"
#include "IElementModelShape.h"
#include "IElementSnapStrategy.h"
#include "IElementSnapComponent.h"
#include "IGraphicsElementShape.h"
#include "IInteractionSnapManager.h"
#include "GraphicsNodeAndTransform.h"
#include "IGraphicsNodePathCollector.h"
#include "ElementSnapStrategyRegister.h"

#include "PointSnapStrategy.h"
#include "CmdSnapModuleEntry.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h" 

using namespace gcmp;
using namespace Sample;

// 远程捕捉自定义点为空
std::vector<gcmp::RemoteSnapData> ModelLineSnapStrategy::GetRemoteSnappableGeometry(const IModelView* pModelView, const IElement* pElement) const
{
    std::vector<gcmp::RemoteSnapData> oRes;

    // 获得IModelLine
    const IGenericElement* pGenericElement = quick_cast<IGenericElement>(pElement);
    DBG_WARN_AND_RETURN_UNLESS(pGenericElement, oRes, L"pGenericElement为空",L"GDMP",L"2023-12-30");
    const IModelLine* pModelLine = quick_cast<IModelLine>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_UNLESS(pModelLine, oRes, L"pModelLine为空",L"GDMP",L"2023-12-30");

    // 获得几何线
    const OwnerPtr<ICurve3d> opCurve = pModelLine->GetGeometryCurve();
    if (opCurve)
    {
        oRes.emplace_back(gcmp::RemoteSnapData(TransferOwnership(opCurve->Clone()), -1));
    }

    return oRes;
}

/// 获得模型线的几何线，计算等分点作为自定义捕捉点
void ModelLineSnapStrategy::GetLocalCustomPointSnapData(const gcmp::IElement* pElement, const gcmp::IModelView* pModelView, std::vector<gcmp::LocalCustomPointSnapData>& snapDatas) const
{
    // 从Ribbon获得等分个数，大于1才计算等分点作为自定义捕捉点
    int divCount = CmdSnapModuleEntry::GetModelLinePointCount();
    if (divCount <= 1)
    {
        return;
    }

    // 获得IModelLine
    const IGenericElement* pGenericElement = quick_cast<IGenericElement>(pElement);
    DBG_WARN_AND_RETURN_VOID_UNLESS(pGenericElement, L"pGenericElement为空",L"GDMP",L"2023-12-30");
    const IModelLine* pModelLine = quick_cast<IModelLine>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_VOID_UNLESS(pModelLine, L"pModelLine为空",L"GDMP",L"2023-12-30");

    // 获得几何线
    const OwnerPtr<ICurve3d> opCurve = pModelLine->GetGeometryCurve();
    DBG_WARN_AND_RETURN_VOID_UNLESS(opCurve, L"opCurve为空",L"GDMP",L"2023-12-30");

    // 获得曲线的参数区间长度。注意不用opCurve->GetLength()，那样不同的曲线计算等分的方式不同
    double limitsLength = opCurve->GetLimits().Length();

    // 避免退化的线
    if (limitsLength <= Constants::MIN_MODEL_SIZE)
    {
        return;
    }

    // 设置捕捉光标
    std::wstring cursorPath = L"";
    if (CmdSnapModuleEntry::DoCustomizeCursor())
    {
        cursorPath = L":/images/Cursor/cross_big.png";
    }

    for (int i = 1; i < divCount; i++)
    {    
        Vector3d snapPoint = opCurve->GetPoint(limitsLength * i / divCount + opCurve->GetStartLimit());
        LocalCustomPointSnapData snapData = LocalCustomPointSnapData(SnapTypeForCustomPoint::CustomPoint, 
            snapPoint, StringUtil::ToWString(i) + L"等分点", {}, cursorPath);
        snapDatas.emplace_back(snapData);
    }

}

REGISTER_ELEMENT_SNAP_STRATEGY(IModelLine, ModelLineSnapStrategy);

